<template>
  <div class="subject-categories-container">
    <div class="categories-header">
      <h2 class="section-title">学科分类</h2>
      <el-button type="text" class="more-btn" @click="handleExpandClick">
        {{ isExpanded ? '收起' : '更多' }}
        <el-icon class="more-icon" :class="{ 'is-rotate': isExpanded }">
          <ArrowDown />
        </el-icon>
      </el-button>
    </div>
    
    <div class="categories-list" :class="{ 'expanded': isExpanded }">
      <div 
        v-for="(category, index) in categories" 
        :key="index" 
        class="category-item"
        @click="navigateToCategory(category)"
      >
        <div class="category-main">
          <div class="category-title">
            <span class="category-name">{{ category.name }}</span>
          </div>
          
          <div class="category-subjects">
            <span 
              v-for="(subject, sIndex) in getSubjectList(category)" 
              :key="sIndex" 
              class="subject-tag"
              @click.stop="navigateToSubCategory(subject)"
            >
              {{ subject.name }}
            </span>
            <span v-if="hasMoreSubjects(category)" class="more-subjects">更多...</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { ArrowDown } from '@element-plus/icons-vue';

const store = useStore();
const router = useRouter();
const isExpanded = ref(false);

// 从 store 获取学科分类
const categories = computed(() => store.state.home.subjectCategories);

// 处理展开/收起点击
const handleExpandClick = () => {
  isExpanded.value = !isExpanded.value;
  console.log('展开状态:', isExpanded.value);
};

// 获取每个分类下的学科列表（用于显示在分类项中）
const getSubjectList = (category) => {
  if (!category.children || category.children.length === 0) {
    return [];
  }
  
  // 只返回前3个子分类，确保不超过一行
  return category.children.slice(0, 3);
};

// 判断是否有更多子分类
const hasMoreSubjects = (category) => {
  return category.children && category.children.length > 3;
};

// 导航到分类页面
const navigateToCategory = (category) => {
  router.push(`/subject/${category.id}`);
};

// 导航到子分类页面
const navigateToSubCategory = (subject) => {
  router.push(`/subject/${subject.id}`);
};
</script>

<style lang="scss" scoped>
.subject-categories-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  padding: 0;
  width: 100%;
  height: 440px; // 已经设置为440px，与轮播图高度一致
  display: flex;
  flex-direction: column;
  overflow: hidden;
  
  .categories-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #f0f2f5;
    flex-shrink: 0; // 防止头部被压缩
    
    .section-title {
      margin: 0;
      font-size: 18px;
      color: #333;
      font-weight: 500;
    }
    
    .more-btn {
      display: flex;
      align-items: center;
      color: #1e83ec;
      font-size: 14px;
      
      .more-icon {
        margin-left: 4px;
        transition: transform 0.3s;
        
        &.is-rotate {
          transform: rotate(180deg);
        }
      }
    }
  }
  
  .categories-list {
    padding: 10px 20px;
    overflow-y: auto; // 添加垂直滚动条
    flex-grow: 1; // 占用剩余空间
    
    // 默认状态下只显示前7个分类
    .category-item:nth-child(n+8) {
      display: none;
    }
    
    // 展开状态下显示所有分类
    &.expanded .category-item:nth-child(n+8) {
      display: block;
    }
    
    .category-item {
      padding: 12px 0;
      border-bottom: 1px dashed #ebeef5;
      cursor: pointer;
      
      &:last-child {
        border-bottom: none;
      }
      
      &:hover {
        .category-name {
          color: #1e83ec;
        }
      }
      
      .category-main {
        .category-title {
          margin-bottom: 8px;
          
          .category-name {
            font-size: 16px;
            font-weight: 500;
            color: #333;
            transition: color 0.3s;
          }
        }
        
        .category-subjects {
          display: flex;
          flex-wrap: nowrap;
          overflow: hidden;
          
          .subject-tag {
            font-size: 14px;
            color: #666;
            margin-right: 15px;
            position: relative;
            white-space: nowrap;
            
            &:not(:last-child)::after {
              content: '/';
              position: absolute;
              right: -10px;
              color: #ddd;
            }
            
            &:hover {
              color: #1e83ec;
            }
          }
          
          .more-subjects {
            font-size: 14px;
            color: #1e83ec;
            cursor: pointer;
            white-space: nowrap;
          }
        }
      }
    }
  }
}

// 自定义滚动条样式
.categories-list::-webkit-scrollbar {
  width: 6px;
}

.categories-list::-webkit-scrollbar-thumb {
  background-color: #dcdfe6;
  border-radius: 3px;
}

.categories-list::-webkit-scrollbar-track {
  background-color: #f5f7fa;
}
</style> 